package com.example.upload.serviceImpl.file;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.upload.constant.DeleteStatus;
import com.example.upload.constant.UserStatus;
import com.example.upload.entity.file.File;
import com.example.upload.entity.user.User;
import com.example.upload.exception.BusinessRuntimeException;
import com.example.upload.mapper.file.FileMapper;
import com.example.upload.mapper.user.UserMapper;
import com.example.upload.service.file.FileService;
import com.example.upload.minio.MinioFileUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * (File)表服务实现类
 *
 * @author makejava
 * @since 2024-05-28 11:28:10
 */
@Service
public class FileServiceImpl implements FileService {
    @Resource
    private FileMapper fileMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MinioFileUtils minioFileUtils;


    /**
     * 新增数据
     *
     * @param multipartFile 实例对象
     * @return 实例对象
     */
    @Override
    public String insert(MultipartFile multipartFile) {
        String filePath = "";
        String loginIdAsString = StpUtil.getLoginIdAsString();
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPersonId, loginIdAsString).eq(User::getStatus, UserStatus.NORMAL.getCode()).eq(User::getDeleted, DeleteStatus.UNDELETE.getCode()));
        if (ObjectUtils.isEmpty(user)){
            throw new BusinessRuntimeException("用户不存在");
        }
        try {

            filePath = minioFileUtils.uploadFile(multipartFile, user.getBucketName());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        File file = new File();
        file.setFilePath(filePath);
        file.setFileName(multipartFile.getOriginalFilename());
        file.setCreateBy(StpUtil.getLoginIdAsString());
        file.setCreateTime(System.currentTimeMillis());
        file.setUpdateBy(StpUtil.getLoginIdAsString());
        file.setUpdateTime(System.currentTimeMillis());
        file.setDeleted(0);
        fileMapper.insert(file);
        return multipartFile.getOriginalFilename();
    }

    @Override
    public void downLoadFile(String filePath, HttpServletResponse response) {
        String loginIdAsString = StpUtil.getLoginIdAsString();
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPersonId, loginIdAsString).eq(User::getStatus, UserStatus.NORMAL.getCode()).eq(User::getDeleted, DeleteStatus.UNDELETE.getCode()));
        if (ObjectUtils.isEmpty(user)){
            throw new BusinessRuntimeException("用户不存在");
        }
        try {
            minioFileUtils.downLoadFile(filePath, user.getBucketName(), response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public PageInfo<File> getMyFile(Integer page, Integer pageSize,String keyWord) {
        String loginIdAsString = StpUtil.getLoginIdAsString();
        PageHelper.startPage(page,pageSize);
        List<File> files = fileMapper.selectList(new LambdaQueryWrapper<File>().
                eq(File::getCreateBy, loginIdAsString).
                eq(File::getDeleted, DeleteStatus.UNDELETE.getCode()).
                like(StringUtils.isNotBlank(keyWord),File::getFileName,keyWord));
        return PageInfo.of(files);
    }


}
